package com.eqxiu.ieditor.api.impl;

import com.eqxiu.ieditor.api.IRecommondServiceApi;

import com.eqxiu.ieditor.commons.page.PageDTO;
import com.eqxiu.ieditor.dao.RecommondMapper;
import com.eqxiu.ieditor.dto.RecommondDTO;
import com.eqxiu.ieditor.entity.Recommond;
import com.eqxiu.ieditor.request.RecommondRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecommondServiceApiImpl implements IRecommondServiceApi {

    @Autowired
    private RecommondMapper recommondMapper;

   public void addRecommond(RecommondDTO recommondDTO){
        if(null != recommondDTO){
            Recommond recommond = new Recommond();
            BeanUtils.copyProperties(recommondDTO,recommond);
            recommondMapper.insertSelective(recommond);
        }
   }


   public List<RecommondDTO> queryRecommondList(RecommondRequest request){
       List<RecommondDTO> resultList = new ArrayList<>();
       Map map = new HashMap();
       map.put("name",request.getName());
       map.put("type",request.getType());
       map.put("loginName",request.getLoginName());
       map.put("productId",request.getProductId());
       if(StringUtils.isEmpty(request.getOrderFile())){
           map.put("orderFile","sort");
       }else{
           map.put("orderFile",request.getOrderFile());
       }
       if(StringUtils.isEmpty(request.getOrderRule())){
           map.put("orderRule","desc");
       }else{
           map.put("orderRule",request.getOrderRule());
       }
       map.put("status",request.getStatus());
       map.put("recommondUsePosition",request.getRecommondUsePosition());
       map.put("scenceCode",request.getScenceCode());
       List<Recommond> recommondList = recommondMapper.getRecommondList(map);
       for(Recommond recommond:recommondList){
           RecommondDTO recommondDTO = new RecommondDTO();
           BeanUtils.copyProperties(recommond,recommondDTO);
           resultList.add(recommondDTO);
       }
       return resultList;
   }


   public  void deleteRecommond(List<Integer> idlist){
        for(Integer id:idlist){
            //删除推荐内容
            recommondMapper.deleteByPrimaryKey(id);
        }
   }

   public void deleteRecommond(Integer id){
       //删除推荐内容
       recommondMapper.deleteByPrimaryKey(id);
   }


    public void updateRecommond(RecommondDTO recommondDTO){
        if(null != recommondDTO){
            Recommond recommond = new Recommond();
            BeanUtils.copyProperties(recommondDTO,recommond);
            recommondMapper.updateByPrimaryKeySelective(recommond);
        }
   }


  public  RecommondDTO getRecommondById(int id){
      RecommondDTO recommondDTO = new RecommondDTO();
      Recommond recommond = recommondMapper.selectByPrimaryKey(id);
      if(null != recommond){
          BeanUtils.copyProperties(recommond,recommondDTO);
      }
      return recommondDTO;
  }


    public PageDTO<RecommondDTO> queryRecommondPage(RecommondRequest request){
      PageDTO<RecommondDTO> pageDTO = new PageDTO<RecommondDTO>();
      List<RecommondDTO> resultList = new ArrayList<>();
      Map map = new HashMap();
      try{
          map.put("name",request.getName());
          map.put("type",request.getType());
          map.put("loginName",request.getLoginName());
          map.put("productId",request.getProductId());
          if(StringUtils.isEmpty(request.getOrderFile())){
              map.put("orderFile","sort");
          }else{
              map.put("orderFile",request.getOrderFile());
          }
          if(StringUtils.isEmpty(request.getOrderRule())){
              map.put("orderRule","desc");
          }else{
              map.put("orderRule",request.getOrderRule());
          }
          map.put("status",request.getStatus());
          map.put("recommondUsePosition",request.getRecommondUsePosition());
          map.put("scenceCode",request.getScenceCode());
          long count =  recommondMapper.getRecommondListCount(map);

          if(!StringUtils.isEmpty(request.getPageNo()) && !StringUtils.isEmpty(request.getPageSize())){
              map.put("limitStart",(request.getPageNo()-1)*request.getPageSize());
              map.put("limitEnd",request.getPageSize());
          }

          List<Recommond> recommondList = recommondMapper.getRecommondList(map);
          for(Recommond recommond:recommondList){
              RecommondDTO recommondDTO = new RecommondDTO();
              BeanUtils.copyProperties(recommond,recommondDTO);
              resultList.add(recommondDTO);
          }
          pageDTO.setCount(count);
          pageDTO.setList(resultList);
          pageDTO.setPageNo(request.getPageNo());
          pageDTO.setPageSize(request.getPageSize());
      }catch (Exception e){
          e.printStackTrace();
      }
      return pageDTO;
  }

   public long countRecommond(RecommondRequest request){
       Map map = new HashMap();
       map.put("name",request.getName());
       map.put("type",request.getType());
       map.put("loginName",request.getLoginName());
       map.put("productId",request.getProductId());
       if(StringUtils.isEmpty(request.getOrderFile())){
           map.put("orderFile","sort");
       }else{
           map.put("orderFile",request.getOrderFile());
       }
       if(StringUtils.isEmpty(request.getOrderRule())){
           map.put("orderRule","desc");
       }else{
           map.put("orderRule",request.getOrderRule());
       }
       map.put("status",request.getStatus());
       map.put("recommondUsePosition",request.getRecommondUsePosition());
       map.put("scenceCode",request.getScenceCode());
       return recommondMapper.getRecommondListCount(map);

   }

}
